package com.gs.tests.seleniumextjs;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.*;

public class GridToForm {
	
	private WebDriver driver;
	private String testfile;
		
	@BeforeTest
	@Parameters({"browser","baseurl","testfile"})
    public void beforeTest(String browser, String url, String file)
    {
		driver = Utils.getDriverInstance(browser);
    	driver.get(url);
    	testfile = file;
    	Utils.maximizeWindow(driver);    	
    }  
    
   @AfterTest
   public void afterTest()
   {
	   driver.quit();
   }
   
   @DataProvider(name = "RecordList")      
   public Object[][] createData() throws Exception {
	   Object[][] retObjArr = Utils.getTestData(testfile);
       return retObjArr;
   }
   
   @Test(description = "drag and drop data from grid to form", dataProvider = "RecordList")
    private void dragAndDrop(String record) throws InterruptedException
 	{
	   // get the grid grow and its data
	   WebElement dragRow = driver.findElement(By.xpath("//tbody[contains(@id,'gridview')]/tr[" + record +"]"));
	   String dragName =dragRow.findElement(By.xpath("td[1]/div")).getText();
	   String dragCol1 =dragRow.findElement(By.xpath("td[2]/div")).getText();
	   String dragCol2 =dragRow.findElement(By.xpath("td[3]/div")).getText();
	   
	   // get target form
	   WebElement dropForm =  driver.findElement(By.xpath("//label[contains(text(),'Record Name')]/ancestor::div[2]"));	 
	   
	   // drag & drop
	   dragRow.click();
	   new Actions(driver).dragAndDrop(dragRow, dropForm).build().perform();	   
	   Thread.sleep(100);
	  
	   // verify data	   
	   WebElement dropName = driver.findElement(By.xpath("//input[@name='name']"));
	   Assert.assertTrue(dropName.getAttribute("value").equals(dragName));
	   
	   WebElement dropCol1 = driver.findElement(By.xpath("//input[@name='column1']"));
	   Assert.assertTrue(dropCol1.getAttribute("value").equals(dragCol1));
	   
	   WebElement dropCol2 = driver.findElement(By.xpath("//input[@name='column2']"));
	   Assert.assertTrue(dropCol2.getAttribute("value").equals(dragCol2));
 	}
}
